// component/countTime1/countTime.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    //时间控件ID
    id: {
      type: Number,
      value: 1 //默认值
    },
    //当前时间
    currentTime: {
      type: Number,
      observer: function (newVal, oldVal) {
        // console.log(newVal, oldVal)
        // this.gogogo();
      }
    },
    // 活动开始时间
    startTime: {
      type: Number,
      // observer: function (newVal, oldVal) { console.log(newVal, oldVal) }
    },
    // 活动结束时间
    endTime: {
      type: Number
    },
    // 倒计时结束显示文本
    endText: {
      type: String,
      value: '已结束'
    },
    //自定义显示文字:天
    dayTxt: {
      type: String,
      value: ':'
    },
    //自定义显示文字:时
    hourTxt: {
      type: String,
      value: ':'
    },
    //自定义显示文字:分
    minutesTxt: {
      type: String,
      value: ':'
    },
    secondsTxt: {
      type: String,
      // value: ':'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    msTime: {			//倒计时数值
      show: false,		//倒计时状态
      day: '',			//天
      hour: '',		//小时
      minutes: '',		//分钟
      seconds: ''		//秒
    },
    star: '',			//活动开始时间
    end: '',				//活动结束时间
    current: '',         //当前时间
  },

  /**
   * 组件的方法列表
   */
  created: function () {
  },
  attached: function () {
    // const _this = this
    this.gogogo();
  },
  methods: {
    gogogo: function () {
      this.properties.startTime.toString().length == 10 ? this.setData({ star: this.properties.startTime * 1000 }) : this.setData({ star: this.properties.startTime * 1000 });
      this.properties.endTime.toString().length == 10 ? this.setData({ end: this.properties.endTime * 1000 }) : this.setData({ end: this.properties.endTime * 1000 });
      // console.log(this.data.star, this.data.end, this.properties.currentTime)
      if (this.properties.currentTime) {
        this.properties.currentTime.toString().length == 10 ? this.setData({ current: this.properties.currentTime * 1000 }) : this.setData({ current: this.properties.currentTime * 1000 });
      } else {
        this.current = (new Date()).getTime();
      }
      if (this.data.end < this.data.current) {

        /**
         * 结束时间小于当前时间 活动已结束
         */
        this.end_message();
      }
      else if (this.data.current < this.data.star) {

        /**
         * 当前时间小于开始时间 活动尚未开始
         */
        setTimeout(() => {
          this.runTime(this.data.star, this.data.current, this.start_message);
        }, 1);
      }
      else if (this.data.end > this.data.current && this.data.star < this.data.current || this.data.star == this.data.current) {
        /**
         * 结束时间大于当前并且开始时间小于当前时间，执行活动开始倒计时
         */
        this.setData({ 'msTime.show': true })
        this.triggerEvent('start_callback', this.data.msTime.show);
        setTimeout(() => {
          this.runTime(this.data.end, this.data.star, this.end_message, true)
        }, 1);
      }
    },
    runTime(startTime, endTime, callFun, type) {
      let msTime = this.data.msTime;
      let timeDistance = startTime - endTime;
      if (timeDistance > 0) {
        this.data.msTime.show = true;
        msTime.day = Math.floor(timeDistance / 86400000);
        timeDistance -= msTime.day * 86400000;
        msTime.hour = Math.floor(timeDistance / 3600000);
        timeDistance -= msTime.hour * 3600000;
        msTime.minutes = Math.floor(timeDistance / 60000);
        timeDistance -= msTime.minutes * 60000;
        //是否开启秒表倒计,未完成
        //        this.secondsFixed ? msTime.seconds = new Number(timeDistance / 1000).toFixed(2) : msTime.seconds = Math.floor( timeDistance / 1000 ).toFixed(0);
        msTime.seconds = Math.floor(timeDistance / 1000).toFixed(0);
        timeDistance -= msTime.seconds * 1000;

        if (msTime.hour < 10) {
          msTime.hour = "0" + msTime.hour;
        }
        if (msTime.minutes < 10) {
          msTime.minutes = "0" + msTime.minutes;
        }
        if (msTime.seconds < 10) {
          msTime.seconds = "0" + msTime.seconds;
        }
        let _s = Date.now();
        let _e = Date.now();
        let diffPerFunc = _e - _s;
        setTimeout(() => {
          if (type) {
            this.runTime(this.data.end, endTime += 1000, callFun, true);
          } else {
            this.runTime(this.data.star, endTime += 1000, callFun);
          }
        }, 1000 - diffPerFunc)
        // console.log(this.data.msTime.day, this.data.msTime.hour, this.properties.msTime.seconds)
        this.setData({ 'msTime.day': msTime.day, 'msTime.hour': msTime.hour, 'msTime.minutes': msTime.minutes, 'msTime.seconds': msTime.seconds })
      }
      else {
        return
        callFun();
      }
    },
    start_message() {
      this.triggerEvent('start_callback', this.data.msTime.show);
      setTimeout(() => {
        this.runTime(this.data.end, this.data.star, this.data.end_message, true)
      }, 1);
    },
    end_message() {
      this.data.msTime.show = false;
      if (this.properties.currentTime <= 0) {
        return;
      }
      this.triggerEvent('end_callback', this.data.msTime.show);
    }
  }
})
